package com.yuntongxun.ecdemo.voicechat.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.squareup.picasso.Picasso;
import com.yuntongxun.ecdemo.R;
import com.yuntongxun.ecdemo.voicechat.bean.Gift;
import com.yuntongxun.ecdemo.voicechat.bean.UserInfo;
import com.yuntongxun.ecdemo.voicechat.listeners.IGiftSelectItemListener;
import com.yuntongxun.ecdemo.voicechat.listeners.IRecyclerViewItemListener;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;


/**
 * Created by Will on 12/9/2015.
 */
public class GiftsSystemAdapter extends RecyclerView.Adapter {

    private Context mContext;

    private List<Gift> notes;

    private IGiftSelectItemListener mListener;

    public GiftsSystemAdapter(Context context, List<Gift> noteList, IGiftSelectItemListener myItemClickListener) {

        mContext = context;

        this.notes = noteList;

        mListener = myItemClickListener;
    }

    @Override
    public int getItemCount() {
        return notes.size();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int type) {
        View v = View.inflate(viewGroup.getContext(), R.layout.adapter_gifts_system_list_item, null);

        return new HomeFragmentViewHolder(v);
    }

    @Override
    public int getItemViewType(int position) {
        return position % 4;  //any logic can go here
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {

        HomeFragmentViewHolder holder = (HomeFragmentViewHolder) viewHolder;

        Gift userInfo = notes.get(position);

        holder.giftNameTv.setText(userInfo.getName() + "");
        holder.coinsNumTv.setText(userInfo.getAmount() + "");
//        Glide.with(mContext).load(userInfo.getUrl()).into(holder.giftImageIv);

        if(userInfo.isSelect()){
            //选中
            holder.giftSelectBgLayout.setBackgroundResource(R.drawable.selected_gift_bg);
        }else{
            holder.giftSelectBgLayout.setBackgroundResource(R.color.white);
        }

        Picasso.with(mContext)
                .load(userInfo.getUrl())
                .placeholder(R.drawable.ic_register_logo)
                .tag(mContext)
                .into(holder.giftImageIv);

    }

    public class HomeFragmentViewHolder extends RecyclerView.ViewHolder {

        @InjectView(R.id.gift_image_iv)
        ImageView giftImageIv;
        @InjectView(R.id.gift_name_tv)
        TextView giftNameTv;
        @InjectView(R.id.coins_num_tv)
        TextView coinsNumTv;

        @InjectView(R.id.gift_select_bg_layout)
        RelativeLayout giftSelectBgLayout;

        public HomeFragmentViewHolder(View itemView) {
            super(itemView);

            ButterKnife.inject(this, itemView);
        }

        @OnClick(R.id.gift_select_bg_layout)
        public void clickItem() {
            if (mListener != null) {
                mListener.onItemClick(getPosition());
            }
        }
    }

}
